#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/11/2 17:22
# @Author  : summer
# @Email   : 610658552@qq.com
# @File    : view_wechat_log.py
# @Software: PyCharm
# @Desc    : 微信公众号发送记录查询视图
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.generics import GenericAPIView

from common.utils.pagination import EnhancedPageNumberPagination
from common.utils.rsp import success_response
from wechat.filters.filter_wechat_log import WechatLogFilter
from wechat.models import WechatLog
from wechat.sers.ser_wechat_log import WechatLogSerializer


class WechatLogListView(GenericAPIView):
    """
    微信公众号发送记录查询接口
    - GET: 分页查询微信发送记录，支持多条件筛选和排序
    """
    # 查询集：只返回未删除、非压测的数据
    queryset = WechatLog.objects.filter(is_delete=0, pt_flag=0)
    serializer_class = WechatLogSerializer
    pagination_class = EnhancedPageNumberPagination  # 复用已有的分页器
    # 过滤后端：支持条件筛选和排序
    filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
    filterset_class = WechatLogFilter  # 绑定过滤器
    # 支持排序的字段（按发送时间、创建时间等）
    ordering_fields = ["send_time", "create_time", "wechat_name", "phone"]
    # 默认排序：按发送时间倒序（最新的在前）
    ordering = ["-send_time"]

    def get(self, request):
        """查询微信公众号发送记录列表"""
        # 应用过滤条件
        filtered_qs = self.filter_queryset(self.get_queryset())
        # 应用分页
        page_qs = self.paginate_queryset(filtered_qs)
        # 序列化数据
        serializer = self.get_serializer(page_qs, many=True)
        # 组装分页响应
        paginated_data = self.paginator.get_paginated_response(serializer.data).data
        return success_response(data=paginated_data, message="微信公众号发送记录查询成功")